package util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//
//import java.util.ArrayList;
//import java.util.Collections;
//import java.util.Comparator;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//
//import matrix.db.AttributeType;
//import matrix.db.AttributeTypeList;
//import matrix.db.BusinessType;
//import matrix.db.BusinessTypeList;
//import matrix.db.Context;
//import matrix.db.ErrorObject;
//import matrix.db.RelationshipType;
//import matrix.db.RelationshipTypeList;
//import adapletplugin.Activator;
//
public class MatrixUtils
{
	private static List<String> fTypeList;
	private static List<String> fRelationshipList;
	private static List<String> fInterfaceList;
	private static List<String> fAttributeList;

	private static Map<String, List<String>> fTypeAttributes;
	private static Map<String, List<String>> fRelationshipAttributes;
	private static Map<String, List<String>> fInterfaceAttributes;

	private static Map<String, String> fAttributeTypes;
	
	private static final Comparator<String> caseInsenComp = new Comparator<String>() {

		@Override
		public int compare(String o1, String o2)
		{
			return o1.compareToIgnoreCase(o2);
		}
	}; 

	public static void loadTypesFromFile(String filename)
	{
		try
		{
			BufferedReader fileReader = new BufferedReader(new FileReader(new File(filename)));

			fTypeList = new ArrayList<String>();
			fTypeAttributes = new HashMap<String, List<String>>();

			String line = fileReader.readLine();
			while (line != null)
			{
				String[] splitLine = line.split("\\|");

				String typeName = splitLine[0];
				fTypeList.add(typeName);

				List<String> attrList = new ArrayList<String>();

				for (int i = 1; i < splitLine.length; i++)
				{
					String attrName = splitLine[i];
					attrList.add(attrName);
				}

				Collections.sort(attrList, caseInsenComp);
				fTypeAttributes.put(typeName, attrList);

				line = fileReader.readLine();
			}

			Collections.sort(fTypeList, caseInsenComp);
			
			fileReader.close();
		} catch (Exception e)
		{
			// TODO: handle exception
			e.printStackTrace();
		}
	}

	public static void loadRelationshipsFromFile(String filename)
	{
		try
		{
			BufferedReader fileReader = new BufferedReader(new FileReader(new File(filename)));

			fRelationshipList = new ArrayList<String>();
			fRelationshipAttributes = new HashMap<String, List<String>>();

			String line = fileReader.readLine();
			while (line != null)
			{
				String[] splitLine = line.split("\\|");

				String relName = splitLine[0];
				fRelationshipList.add(relName);

				List<String> attrList = new ArrayList<String>();

				for (int i = 1; i < splitLine.length; i++)
				{
					String attrName = splitLine[i];
					attrList.add(attrName);
				}

				Collections.sort(attrList, caseInsenComp);
				fRelationshipAttributes.put(relName, attrList);

				line = fileReader.readLine();
			}

			Collections.sort(fRelationshipList, caseInsenComp);
			
			fileReader.close();
		} catch (Exception e)
		{
			// TODO: handle exception
			e.printStackTrace();
		}
	}

	public static void loadInterfacesFromFile(String filename)
	{
		try
		{
			BufferedReader fileReader = new BufferedReader(new FileReader(new File(filename)));

			fInterfaceList = new ArrayList<String>();
			fInterfaceAttributes = new HashMap<String, List<String>>();

			String line = fileReader.readLine();
			while (line != null)
			{
				String[] splitLine = line.split("\\|");

				String interfaceName = splitLine[0];
				fInterfaceList.add(interfaceName);

				List<String> attrList = new ArrayList<String>();

				for (int i = 1; i < splitLine.length; i++)
				{
					String attrName = splitLine[i];
					attrList.add(attrName);
				}

				Collections.sort(attrList, caseInsenComp);
				fInterfaceAttributes.put(interfaceName, attrList);

				line = fileReader.readLine();
			}

			Collections.sort(fInterfaceList, caseInsenComp);
			
			fileReader.close();
		} catch (Exception e)
		{
			// TODO: handle exception
			e.printStackTrace();
		}
	}

	public static void loadAttributesFromFile(String filename)
	{
		try
		{
			BufferedReader fileReader = new BufferedReader(new FileReader(new File(filename)));

			fAttributeList = new ArrayList<String>();
			fAttributeTypes = new HashMap<String, String>();

			String line = fileReader.readLine();
			while (line != null)
			{
				String[] splitLine = line.split("\\|");

				String attributeName = splitLine[0];
				String attributeType = splitLine[1];
				
				fAttributeList.add(attributeName);
				fAttributeTypes.put(attributeName, attributeType);

				line = fileReader.readLine();
			}

			Collections.sort(fAttributeList, caseInsenComp);
			
			fileReader.close();
		} catch (Exception e)
		{
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	
	public static List<String> getTypes()
	{
		return fTypeList;
	}
	
	public static List<String> getRelationships()
	{
		return fRelationshipList;
	}
	
	public static List<String> getInterfaces()
	{
		return fInterfaceList;
	}
	
	public static List<String> getAttributes()
	{
		return fAttributeList;
	}
	
	public static List<String> getTypeAttributes(String type)
	{
		return fTypeAttributes == null ? null : fTypeAttributes.get(type);
	}
	
	public static List<String> getRelationshipAttributes(String relationship)
	{
		return fRelationshipAttributes == null ? null : fRelationshipAttributes.get(relationship);
	}
	
	public static List<String> getInterfaceAttributes(String interf)
	{
		return fInterfaceAttributes == null ? null : fInterfaceAttributes.get(interf);
	}
	
	public static String getAttributeType(String attribute)
	{
		return fAttributeTypes == null ? null : fAttributeTypes.get(attribute);
	}
	
	// private static BusinessTypeList typeList;
	// private static RelationshipTypeList relationList;
	// private static AttributeTypeList attributeList;
	//
	// private static List<BusinessType> sortedTypeList;
	// private static List<RelationshipType> sortedRelationList;
	// private static List<AttributeType> sortedAttributeList;
	//
	// private static Map<String, List<AttributeType>> attributesPerType;
	//
	// public static final Comparator<ErrorObject> errorObjectComparator = new
	// Comparator<ErrorObject>(){
	//
	// @Override
	// public int compare(ErrorObject o1, ErrorObject o2)
	// {
	// return o1.getName().compareToIgnoreCase(o2.getName());
	// }
	//
	// };
	//
	// private static void downloadTypeInfo() throws Exception
	// {
	// Context context = Activator.getDefault().getContext();
	//
	// if(typeList == null)
	// {
	// typeList = BusinessType.getBusinessTypes(context, true);
	//
	// sortedTypeList = new ArrayList<BusinessType>();
	//
	// for(Object busType : typeList)
	// {
	// sortedTypeList.add((BusinessType) busType);
	// }
	//
	// Collections.sort(sortedTypeList, errorObjectComparator);
	// }
	//
	//
	// }
	//
	// private static void downloadRelationInfo() throws Exception
	// {
	// Context context = Activator.getDefault().getContext();
	//
	// if(relationList == null)
	// {
	// relationList = RelationshipType.getRelationshipTypes(context, true);
	//
	// sortedRelationList = new ArrayList<RelationshipType>();
	//
	// for(Object relType : relationList)
	// {
	// sortedRelationList.add((RelationshipType) relType);
	// }
	//
	// Collections.sort(sortedRelationList, errorObjectComparator);
	// }
	//
	//
	// }
	//
	// private static void downloadAttributeInfo() throws Exception
	// {
	// Context context = Activator.getDefault().getContext();
	//
	// if(attributeList == null)
	// {
	// attributeList = AttributeType.getAttributeTypes(context, true);
	//
	// sortedAttributeList = new ArrayList<AttributeType>();
	//
	// for(Object attrType : attributeList)
	// {
	// sortedAttributeList.add((AttributeType) attrType);
	// }
	//
	// Collections.sort(sortedAttributeList, errorObjectComparator);
	// }
	// }
	//
	// public static List<BusinessType> getTypeList() throws Exception
	// {
	// if(sortedTypeList == null)
	// {
	// downloadTypeInfo();
	// }
	//
	// return sortedTypeList;
	// }
	//
	// public static List<RelationshipType> getRelationList() throws Exception
	// {
	// if(sortedRelationList == null)
	// {
	// downloadRelationInfo();
	// }
	//
	// return sortedRelationList;
	// }
	//
	// public static List<AttributeType> getAttributeList() throws Exception
	// {
	// if(sortedAttributeList == null)
	// {
	// downloadAttributeInfo();
	// }
	// return sortedAttributeList;
	// }
	//
	// public static List<AttributeType> getAttributesForType(String typeName)
	// {
	// return attributesPerType.get(typeName);
	// }
	//
	// public static void retrieveEnoviaSchema()
	// {
	// try{
	// downloadAttributeInfo();
	// downloadRelationInfo();
	// downloadTypeInfo();
	//
	// downloadTypeAttributeInfo();
	// }catch(Exception e)
	// {
	// //TODO Catch Exception
	// System.out.println("Error downloading Schema");
	// }
	// }
	//
	// private static void downloadTypeAttributeInfo() throws Exception
	// {
	// attributesPerType = new HashMap<String, List<AttributeType>>();
	//
	// Context context = Activator.getDefault().getContext();
	//
	// for(BusinessType type : sortedTypeList)
	// {
	// AttributeTypeList attributes = type.getAttributeTypes(context);
	// List<AttributeType> tempList = new ArrayList<AttributeType>();
	//
	// for(Object attrObj : attributes)
	// {
	// tempList.add((AttributeType) attrObj);
	// }
	//
	// Collections.sort(tempList, errorObjectComparator);
	//
	// attributesPerType.put(type.getName(), tempList);
	// }
	// }
}
